package com.bdqn.day0106.service.impl;

import com.bdqn.day0106.mybatis.entity.User;
import com.bdqn.day0106.mybatis.mapper.UserMapper;
import com.bdqn.day0106.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author:Corday
 * @Date:2022/01/06/14:18
 */
@Service
public class UserServiceImpl implements UserService {
    //注入一个对象
    @Resource
    private UserMapper userMapper;

    /**
     * @param uname
     * @param pass
     * @return
     */
    @Override
    public boolean findByUnameAndPass(String uname, String pass) {
        Integer flag = userMapper.selectByUnameAndPassword(uname, pass);
        //如果没有此用户则返回false,有则返回true
        return flag != null;
    }

    /**
     * 以下两个方法请完成
     *
     * @param uname
     * @return
     */
    @Override
    public boolean findByUname(String uname) {
        //如果用户不存在，则返回null
        Integer flag = userMapper.selectByUname(uname);
        //为false不存在，为true存在
        return flag != null;
    }

    /**
     * @param user
     * @return
     */
    @Override
    public String saveUser(User user) {
        boolean flag = findByUname(user.getUname());
        if (!flag) {
            //执行注册
            userMapper.insertUser(user);
            return "亲，注册成功!";
        }
        return "亲，用户名已存在!";
    }
}
